package com.th.supcom.pds.drugreturn.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid.ColumnConfigEnhance;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * @author yzhang
 */

public class RefundApplicationPrescDetailPanel extends LayoutContainer
{
    /** 处方明细表格. */
    private PrescDetailGrid <PdsPrescDetailInfoModel> m_prescDetailGrid;

    public RefundApplicationPrescDetailPanel ()
    {
        setBorders (true);
        setLayout (new FitLayout ());
        initUI ();
    }

    public PrescDetailGrid <PdsPrescDetailInfoModel> getPrescDetailGrid ()
    {
        return m_prescDetailGrid;
    }

    /**
     * 初始化UI界面
     */
    private void initUI ()
    {

        m_prescDetailGrid = new PrescDetailGrid <PdsPrescDetailInfoModel> (new ColumnConfigEnhance ()
        {
            @Override
            public void enhance (final List <ColumnConfig> configs)
            {
                configs.remove (5);
                configs.remove (5);
                configs.remove (7);
                configs.get (3).setWidth (60);
                configs.get (4).setWidth (60);
                configs.get (5).setWidth (50);
                configs.get (6).setWidth (50);
                ColumnConfig column = new ColumnConfig ();
                SimpleMethod.setColumnConfig (column, "drugRepercussionMemo", "退药原因", HorizontalAlignment.LEFT, 90, null);
                column = new ColumnConfig ();
                column.setId ("phamBatchNumber");
                column.setHeader ("批次");
                column.setWidth (60);
                configs.add (column);
                column = new ColumnConfig ();
                column.setId ("receiptDate");
                column.setHeader ("有效期");
                column.setWidth (80);
                column.setDateTimeFormat (DateTimeFormat.getFormat (CodeConstants.DATE_FORMAT_YYYY_MM_DD));
                configs.add (column);
            }
        });
        m_prescDetailGrid.getGrid ().getView ().setForceFit (false);
        m_prescDetailGrid.getGrid ().getStore ().addListener (Store.Add, new Listener <StoreEvent <PdsPrescDetailInfoModel>> ()
        {
            @Override
            public void handleEvent (final StoreEvent <PdsPrescDetailInfoModel> be)
            {
                boolean isHidden = true;
                for (final PdsPrescDetailInfoModel model : be.getModels ())
                {
                    if (model.getPhamBatchNumber () != null)
                    {
                        isHidden = false;
                        break;
                    }
                }
                final ColumnConfig phamBatchNumberColumn = m_prescDetailGrid.getGrid ().getColumnModel ().getColumnById ("phamBatchNumber");
                final ColumnConfig receiptDateColumn = m_prescDetailGrid.getGrid ().getColumnModel ().getColumnById ("receiptDate");
                phamBatchNumberColumn.setHidden (isHidden);
                receiptDateColumn.setHidden (isHidden);
                m_prescDetailGrid.getGrid ().getView ().refresh (true);
            };
        });
        add (m_prescDetailGrid);
    }

}
